package com.qf.flash.ui.circle;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.qf.flash.R;
import com.qf.flash.adapter.FunnyAdapter;
import com.qf.flash.base.BaseRefreshFragment;
import com.qf.flash.model.LifeFunBean;
import com.qf.flash.mvp.contract.LifeFunContract;
import com.qf.flash.mvp.presenter.LifeFunPresenter;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.widget.ViewUtil;

import java.util.List;

import static com.qf.flash.constant.Constant.LIFE_OR_FUN;

/**
 * 圈子模块   ---->   搞笑
 */
public class FunnyFragment extends BaseRefreshFragment implements LifeFunContract.View {
    private RVAdapter<Object> mAdapter;
    private LifeFunPresenter lifeFunPresenter;
    private String type;
    int pageNo = 1, pagesize = 30;
    @Override
    protected void requestData(boolean isRefresh, int page) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        lifeFunPresenter.getLifeList(type, pageNo, pagesize);
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    @Override
    public RecyclerView.Adapter getRecyclerAdapter() {
        mAdapter = new RVAdapter<>(new FunnyAdapter());
        return mAdapter;
    }

    @Override
    protected void lazyLoad() {
        mRefresh.setBackgroundColor(getResources().getColor(R.color.common_gray_bg));
        mRefresh.autoRefresh();
//        lifeFunPresenter.getLifeList(type, 1, 30);
    }

    public static FunnyFragment newInstance(String type) {
        FunnyFragment recommendFragment = new FunnyFragment();
        Bundle bundle = new Bundle();
        bundle.putString(LIFE_OR_FUN, type);
        recommendFragment.setArguments(bundle);
        return recommendFragment;
    }


    @Override
    protected void initInject() {
        super.initInject();
        lifeFunPresenter = new LifeFunPresenter();
        initPresenters(lifeFunPresenter);
        Bundle bundle = getArguments();
        type = bundle.getString(LIFE_OR_FUN);
    }


    @Override
    public void onError(ApiException e) {
        super.onError(e);
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
    }

    @Override
    public void setLifeListData(List<LifeFunBean> lifeFunBeans, int totalPage) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        mRefresh.setEnableLoadMore(page  < totalPage);
        List<Object> datas = mAdapter.getDatas();
        if (isRefresh) {
            datas.clear();
        }
        datas.addAll(lifeFunBeans);
        mAdapter.notifyDataSetChanged();
    }
}
